﻿using ServerSocket.server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerSocket.entity
{

    enum RoomState
    {
        等待加入,
        准备战斗,
        战斗中,
        战斗结束
    }

    class Room
    {

        /// <summary>
        /// 房间ID
        /// </summary>
        public string Id;
        /// <summary>
        /// 房间名字
        /// </summary>
        public string Name;

        /// <summary>
        /// 房间状态
        /// </summary>
        private RoomState state;
        
        /// <summary>
        /// 回合
        /// </summary>
        public int Round { get; set; } = 0;

        /// <summary>
        /// 服务类
        /// </summary>
        private Server server;

        /// <summary>
        /// 这个房间的所有Client
        /// </summary>
        private List<Client> clientList = new List<Client>();
        public List<Client> ClientList
        {
            get
            {
                return clientList;
            }
            set
            {
                clientList = value;
            }
        }

        /// <summary>
        /// 请求次数
        /// </summary>
        private int requestCount;
        public void InitRequestCount()
        {
            requestCount = 0;
        }
        public int GetRequestCount
        {
            get
            {
                requestCount++;
                return requestCount;
            }
        }

        /// <summary>
        /// 创建房间
        /// </summary>
        /// <param name="server"></param>
        public void CreateRoom(Client client,Server server)
        {
            //将自己加入房间
            AddClient(client);
            //修改房间状态
            state = RoomState.等待加入;
            this.server = server;
        }

        /// <summary>
        /// 房间是否是等待加入
        /// </summary>
        /// <returns></returns>
        public bool Is等待加入
        {
            get
            {
                return RoomState.等待加入 == state;
            }
        }

        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="client"></param>
        public void AddClient(Client client)
        {
            clientList.Add(client);
            if (clientList.Count >= 2)
            {
                state = RoomState.准备战斗;
                new Thread(RunTimer).Start();
            }
        }

        /// <summary>
        /// 加入房间倒计时
        /// </summary>
        private void RunTimer()
        {
            Thread.Sleep(1000);
            for (int i = 3; i > 0; i--)
            {
                BroadcastMessage(null, "time", i.ToString());
                Thread.Sleep(1000);
            }
            state = RoomState.战斗中;
        }

        /// <summary>
        /// 向当前房间中所有客户端发送消息
        /// </summary>
        /// <param name="excludeClient"></param>
        /// <param name="actionCode">方法名</param>
        /// <param name="data">数据</param>
        public void BroadcastMessage(Client excludeClient, string actionCode, string data)
        {
            foreach (Client client in clientList)
            {
                if (client != excludeClient)
                {
                    server.SendResponse(client, actionCode, data);
                }
            }
        }

        /// <summary>
        /// 向当前房间中所有客户端发送消息（序列化）
        /// </summary>
        /// <param name="excludeClient"></param>
        /// <param name="actionCode">方法名</param>
        /// <param name="data">数据</param>
        public void BroadcastMessage<T>(Client excludeClient, string actionCode, T data)
        {
            foreach (Client client in clientList)
            {
                if (client != excludeClient)
                {
                    server.SendResponse(client, actionCode, data);
                }
            }
        }

    }
}
